package com.ruoyi.lock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lock.domain.AType;
import com.ruoyi.lock.service.IATypeService;
import com.ruoyi.utils.login.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 型号管理Controller
 * 
 * @author arvin
 * @date 2025-03-02
 */
@RestController
@RequestMapping("/lock/type")
public class ATypeController extends BaseController
{
    @Autowired
    private IATypeService aTypeService;

    @Value("${app.domain}")
    private String appDomain;

    /**
     * 查询型号管理列表
     */
    @PreAuthorize("@ss.hasPermi('lock:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(AType aType)
    {
        startPage();
        List<AType> list = aTypeService.selectATypeList(aType);
        
        // 为每个型号生成二维码
        for (AType type : list) {
            try {
                // 构建完整的URL
                String url = appDomain + "/problems?typeId=" + type.getId();
                // 生成二维码
                String qrCodeBase64 = QRCodeUtil.createQRCode(url);
                // 设置二维码到型号对象
                type.setQrCode(qrCodeBase64);
            } catch (Exception e) {
                logger.error("生成型号[" + type.getId() + "]二维码失败", e);
                type.setQrCode(null);
            }
        }
        
        return getDataTable(list);
    }

    /**
     * 查询型号管理列表
     */
    @GetMapping("/list2")
    public TableDataInfo list2(AType aType)
    {
        List<AType> list = aTypeService.selectATypeList(aType);
        return getDataTable(list);
    }

    /**
     * 导出型号管理列表
     */
    @PreAuthorize("@ss.hasPermi('lock:type:export')")
    @Log(title = "型号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AType aType)
    {
        List<AType> list = aTypeService.selectATypeList(aType);
        ExcelUtil<AType> util = new ExcelUtil<AType>(AType.class);
        util.exportExcel(response, list, "型号管理数据");
    }

    /**
     * 获取型号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('lock:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aTypeService.selectATypeById(id));
    }

    /**
     * 新增型号管理
     */
    @PreAuthorize("@ss.hasPermi('lock:type:add')")
    @Log(title = "型号管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AType aType)
    {
        return toAjax(aTypeService.insertAType(aType));
    }

    /**
     * 修改型号管理
     */
    @PreAuthorize("@ss.hasPermi('lock:type:edit')")
    @Log(title = "型号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AType aType)
    {
        return toAjax(aTypeService.updateAType(aType));
    }

    /**
     * 删除型号管理
     */
    @PreAuthorize("@ss.hasPermi('lock:type:remove')")
    @Log(title = "型号管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aTypeService.deleteATypeByIds(ids));
    }


}
